﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SystemFrameWork.Enums;
using Entity.Model;
using SystemFrameWork;

namespace DutyWeb.Admin.System
{
    public partial class SysUserAdmin : BasePage
    {
        /// <summary>
        /// edit flag of this page
        /// </summary>
        private bool editFlag
        {
            get
            {
                if (this.LabEditFlag.Text.ToLower() == ConstantsManager.True)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            set
            {
                this.LabEditFlag.Text = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnLoadComplete(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.initialize();
            }
        }

        private void initialize()
        {
            string id = Request.QueryString["id"];
            if (StringHelper.IsGUID(id))
            {
                this.editFlag = true;
                this.LabHidID.Text = id;
                SysUserView user = base.sys_UserManagement.LoadSysUserViewByID(new Guid(id));
                if (user != null)
                {
                    this.txtUserName.Text = user.UserName;
                    this.txtRealName.Text = user.RealName;
                    this.txtTel.Text = user.Tel;
                    this.txtUserComment.Text = user.Description;
                    this.txtPoliceNo.Text = user.IDCard;

                    if (!string.IsNullOrEmpty(user.Sex))
                    {
                        this.rdbSex.SelectedValue = user.Sex.Trim();
                    }

                    this.txtPassWord.Text = user.Pwd;
                    //this.chkIsSuperAdmin.Checked = user.IsSuperAdministrator;
                    if (user.IsSuperAdministrator)
                    {
                        this.Sys_RoleList1.EnableTheControl(false);
                    }
                    this.DepartmentTree1.SelectedDepartmentID = user.DepartmentID;
                    this.DepartmentTree1.SelectedDepartmentName = user.DepartmentName;
                    List<Sys_RoleUser> listRoleUser = base.sys_UserManagement.LoadSys_RoleUser(user.ID);

                    this.Sys_RoleList1.BindedFlag = true;
                    List<Guid> selectedRoleIList = new List<Guid>();
                    foreach (Sys_RoleUser sru in listRoleUser)
                    {
                        selectedRoleIList.Add((Guid)sru.Sys_RoleID);
                    }

                    if (selectedRoleIList.Count > 0)
                    {
                        this.Sys_RoleList1.SelectedRoles = selectedRoleIList;
                    }
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.editFlag)
            {
                this.UpdateSysUser();
            }
            else
            {
                this.AddUser();
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtRealName.Text = string.Empty;
            this.txtUserName.Text = string.Empty;
            this.rdbSex.SelectedIndex = -1;
            this.DepartmentTree1.SelectedDepartmentID = null;
            this.Sys_RoleList1.SelectedRoles = new List<Guid>();
            this.txtTel.Text = string.Empty;
            this.txtUserComment.Text = string.Empty;
            this.txtPoliceNo.Text = string.Empty;
            this.txtPassWord.Text = string.Empty;
            //this.chkIsSuperAdmin.Checked = false;
            this.Sys_RoleList1.EnableTheControl(true);
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateSysUser()
        {
            if (string.IsNullOrEmpty(this.txtRealName.Text.Trim()))
            {
                base.ShowMessage("用户真实姓名不能为空!", ExceptionLevel.Warn);
                return;
            }

            if (this.rdbSex.SelectedIndex == -1)
            {
                base.ShowMessage("性别不能为空!", ExceptionLevel.Warn);
                return;
            }

            if (string.IsNullOrEmpty(this.txtUserName.Text.Trim()))
            {
                base.ShowMessage("用户名不能为空!", ExceptionLevel.Warn);
                return;
            }

            if (string.IsNullOrEmpty(this.txtPassWord.Text.Trim()))
            {
                base.ShowMessage("密码不能为空！", ExceptionLevel.Warn);
                return;
            }

            if (this.DepartmentTree1.SelectedDepartmentID == null || string.IsNullOrEmpty(this.DepartmentTree1.SelectedDepartmentName))
            {
                base.ShowMessage("部门不能为空，请在下拉列表中选择所在部门！", ExceptionLevel.Warn);
                return;
            }

            Sys_User user1 = base.sys_UserManagement.LoadUserByName(this.txtUserName.Text.Trim());
            if (user1 != null && user1.ID.ToString() != this.LabHidID.Text)
            {
                base.ShowMessage("该用户名已经存在，请重新输入！", ExceptionLevel.Warn);
                return;
            }

            Sys_User user = base.sys_UserManagement.LoadSysUserByID(new Guid(this.LabHidID.Text));
            user.RealName = this.txtRealName.Text.Trim();
            user.Sex = this.rdbSex.SelectedValue;
            user.UserName = this.txtUserName.Text.Trim();
            user.Pwd = this.txtPassWord.Text.Trim();
            user.ModifiedOn = DateTime.Now;
            //user.DepartmentID = this.DepartmentTree1.SelectedDepartmentID;
            user.Description = this.txtUserComment.Text.Trim();
            user.Tel = this.txtTel.Text.Trim();
            user.IDCard = this.txtPoliceNo.Text.Trim();
            if (base.CurrentUserID == Guid.Empty)
            {
                base.ShowMessage("登录后才能执行此项操作！", ExceptionLevel.Fatal);
                Response.Redirect("~/login/login.aspx");
            }
            user.ModifiedBy = base.CurrentUserID;
            List<Guid> list = this.Sys_RoleList1.SelectedRoles;
            //user.IsSuperAdministrator = this.chkIsSuperAdmin.Checked;

            List<Sys_RoleUser> listRoleUser = base.sys_UserManagement.LoadSys_RoleUser(user.ID);
            base.sys_UserManagement.Delete(listRoleUser);

            foreach (Guid id in list)
            {
                Sys_RoleUser sru = new Sys_RoleUser();
                sru.ID = Guid.NewGuid();
                sru.ModifiedBy = base.CurrentUserID;
                sru.ModifiedOn = DateTime.Now;
                sru.CreatedBy = base.CurrentUserID;
                sru.CreatedON = DateTime.Now;
                sru.Sys_RoleID = id;
                sru.Sys_UserID = user.ID;
                base.sys_UserManagement.Insert(sru);
            }


            base.sys_UserManagement.UpdateChanges();

            base.ShowMessage("操作成功！", ExceptionLevel.Info);
        }

        /// <summary>
        /// add a new user
        /// </summary>
        private void AddUser()
        {
            if (string.IsNullOrEmpty(this.txtRealName.Text.Trim()))
            {
                base.ShowMessage("用户真实姓名不能为空!", ExceptionLevel.Warn);
                return;
            }

            if (this.rdbSex.SelectedIndex == -1)
            {
                base.ShowMessage("性别不能为空!", ExceptionLevel.Warn);
                return;
            }

            if (string.IsNullOrEmpty(this.txtUserName.Text.Trim()))
            {
                base.ShowMessage("用户名不能为空!", ExceptionLevel.Warn);
                return;
            }

            if (string.IsNullOrEmpty(this.txtPassWord.Text.Trim()))
            {
                base.ShowMessage("密码不能为空！", ExceptionLevel.Warn);
                return;
            }

            if (this.DepartmentTree1.SelectedDepartmentID == null || string.IsNullOrEmpty(this.DepartmentTree1.SelectedDepartmentName))
            {
                base.ShowMessage("部门不能为空，请在下拉列表中选择所在部门！", ExceptionLevel.Warn);
                return;
            }

            Sys_User user1 = base.sys_UserManagement.LoadUserByName(this.txtUserName.Text.Trim());
            if (user1 != null)
            {
                base.ShowMessage("该用户名已经存在，请重新输入！", ExceptionLevel.Warn);
                return;
            }

            Sys_User user = new Sys_User();
            user.ID = Guid.NewGuid();
            user.RealName = this.txtRealName.Text.Trim();
            user.Sex = this.rdbSex.SelectedValue;
            user.UserName = this.txtUserName.Text.Trim();
            user.Pwd = this.txtPassWord.Text.Trim();
            user.CreatedOn = DateTime.Now;
            user.ModifiedOn = DateTime.Now;
            //user.DepartmentID = this.DepartmentTree1.SelectedDepartmentID;
            user.Description = this.txtUserComment.Text.Trim();
            user.Tel = this.txtTel.Text.Trim();
            user.IDCard = this.txtPoliceNo.Text.Trim();
            if (base.CurrentUserID == Guid.Empty)
            {
                base.ShowMessage("登录后才能执行此项操作！", ExceptionLevel.Fatal);
                Response.Redirect("~/login/login.aspx");
            }
            user.ModifiedBy = base.CurrentUserID;
            user.Createdy = base.CurrentUserID;
            List<Guid> list = this.Sys_RoleList1.SelectedRoles;
            //user.IsSuperAdministrator = this.chkIsSuperAdmin.Checked;

            foreach (Guid id in list)
            {
                Sys_RoleUser sru = new Sys_RoleUser();
                sru.ID = Guid.NewGuid();
                sru.ModifiedBy = base.CurrentUserID;
                sru.ModifiedOn = DateTime.Now;
                sru.CreatedBy = base.CurrentUserID;
                sru.CreatedON = DateTime.Now;
                sru.Sys_RoleID = id;
                sru.Sys_UserID = user.ID;
                base.sys_UserManagement.Insert(sru);
            }

            base.sys_UserManagement.Insert(user);
            base.sys_UserManagement.UpdateChanges();

            base.ShowMessage("操作成功！", ExceptionLevel.Info);
            this.btnCancel_Click(null, null);
        }

        protected void chkIsSuperAdmin_CheckedChanged(object sender, EventArgs e)
        {
            //if (this.chkIsSuperAdmin.Checked)
            //{
            //    this.Sys_RoleList1.SelectedRoles = new List<Guid>();
            //    this.Sys_RoleList1.EnableTheControl(false);
            //}
            //else
            //{
            //    this.Sys_RoleList1.EnableTheControl(true);
            //}
        }
    }
}
